$(function() {
    let token = GetQueryString('token');
    let keys = GetQueryString('keys');
    let area_id = GetQueryString('area_id');
    let category_id = GetQueryString('category_id');
    let cat_name = GetQueryString('cat_name');
    let title = GetQueryString('cat_name');

    $('.category_id').attr('data-category_id', category_id);
    $('.cat_name').attr('data-cat_name', cat_name);
    // 默认第一页
    let page = 1;
    // 每页的数量
    let pageSize = 10;
    ajaxList(page, area_id, keys, '', category_id, '');

    //更多品牌
    function ajaxList(page, area_id, keys, first_code, category_id, color_id) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': "Plat|index_search_more_ngoods", 'p': page, 'page_size': pageSize, 'area_id': area_id, 'keywords': keys, 'first_code': first_code, 'category_id': category_id, 'color_id': color_id },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';

                    let cat_name = $('.cat_name').attr('data-cat_name');
                    $('.hetitle').html(title);
                    if (category_id) {
                        $('.classifyName').html(cat_name);
                    }
                    data.list.forEach(item => {
                        // // console.log(item);
                        html += `
                        <a href="home_n_shopClassifyDetail.html?goods_id=${item.id}&token=${token}">
                            <img class="cloneshopImg" src="${item.goods_thumb == '' ? '../img/user.jpg' : item.goods_thumb}" alt="${item.goods_name}">
                            <p class="cloneshopText">${item.goods_name}</p>
                        </a>
                        `;
                    });
                    if (data.list.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.cloneLists').append(html);
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    // 分类
    $('.classifyTab').on('click', function() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': "Plat|block_company_category" },
            dataType: 'json',
            success: function(res) {
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    data.forEach(item => {
                        html += `<a href="javascript:;" data-category_id="${item.id}" data-area_id="${item.area_id}" data-cat_type="${item.cat_type}" class="shList classsifyBtn">${item.cat_name}</a>`
                    })
                    $('.classifyLists').html(html);
                    $('.classsifyBtn').on('click', function() {
                        let cat_type = $(this).attr('data-cat_type');
                        let area_id = $(this).attr('data-area_id');
                        let category_id = $(this).attr('data-category_id');
                        let category_name = $(this).html();
                        $(".category_id").attr("data-category_id", category_id);
                        $('.cat_name').attr('data-cat_name', category_name);
                        if (cat_type == 1) {
                            location.href = "companyLists.html?area_id=" + area_id + "&id=" + category_id + "&cat_name=" + category_name;
                        } else {
                            // 重置
                            $('.cloneLists').html('');
                            $('.notMore').css('display', 'none');
                            page = 1;
                            // 条件
                            let first_code = $('.first_code').attr('data-first_code');
                            let color_id = $('.color_id').attr('data-color_id');
                            // tab框显示对应内容
                            let cat_name = $('.cat_name').attr('data-cat_name');
                            // 点击搜索渲染
                            if (first_code || color_id) {
                                ajaxList(page, area_id, '', first_code, category_id, color_id);
                            } else {
                                ajaxList(page, area_id, '', '', category_id, '');
                            }
                            console.log(cat_name)
                            $('.classifyName').html(cat_name);
                            $('.tabItem').attr("data-num", 1).removeClass('on');
                            dialogClose();
                        }

                    })
                }
            }
        })
    })

    // 字母
    $('.letter26>a').on('click', function() {
        let code = $(this).attr('data-code');
        let first_code = '';
        if (code == 'all') {
            first_code = '';
        } else {
            first_code = $(this).html();
        }
        $('.first_code').attr('data-first_code', first_code);
        // 重置
        $('.cloneLists').html('');
        $('.notMore').css('display', 'none');
        page = 1;
        // 条件
        let category_id = $('.category_id').attr('data-category_id');
        let color_id = $('.color_id').attr('data-color_id');
        if (first_code == "") {
            $('.firstcodeName').html('字母');
        } else {
            $('.firstcodeName').html(first_code);
        }
        // 点击搜索渲染
        if (category_id || color_id) {
            ajaxList(page, area_id, '', first_code, category_id, color_id);
        } else {
            ajaxList(page, area_id, '', first_code, '', '');
        }
        $('.tabItem').attr("data-num", 1).removeClass('on');
        dialogClose();
    })

    // 颜色
    $('.colorTab').on('click', function() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': "Goods|color_list" },
            dataType: 'json',
            success: function(res) {
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    let all = '';
                    all = `
                    <a href="javascript:;" data-color_id="" class="shList colorBtn">全部</a>
                    `;
                    data.forEach(item => {
                        html += `<a href="javascript:;" data-color_id="${item.id}" class="shList colorBtn">${item.color_name}</a>`
                    })
                    $('.colorList').html(all + html);
                    $('.colorBtn').on('click', function() {
                        let color_id = $(this).attr('data-color_id');
                        let color_name = $(this).html();
                        $('.color_id').attr('data-color_id', color_id);
                        // 重置
                        $('.cloneLists').html('')
                        // 条件
                        let first_code = $('.first_code').attr('data-first_code');
                        let category_id = $('.category_id').attr('data-category_id');
                        if (color_id == '') {
                            $('.colorName').html('颜色');
                        } else {
                            $('.colorName').html(color_name);
                        }
                        // 点击搜索渲染
                        if (category_id || first_code) {
                            ajaxList(page, area_id, '', first_code, category_id, color_id);
                        } else {
                            ajaxList(page, area_id, '', '', '', color_id);
                        }
                        $('.tabItem').attr("data-num", 1).removeClass('on');
                        dialogClose();
                    })
                }
            }
        })
    })

    // 搜索
    $('.searchIcon').on('click', function() {
        let keys = $('.inputBtn>input').val();
        $('.keys').attr('data-keys', keys);
        // console.log(keys);
        // 重置
        $('.cloneLists').html('');
        $('.notMore').css('display', 'none');
        page = 1;
        // 条件
        let first_code = $('.first_code').attr('data-first_code');
        let category_id = $('.category_id').attr('data-category_id');
        let color_id = $('.color_id').attr('data-color_id');
        if (first_code || category_id || color_id) {
            ajaxList(page, area_id, keys, first_code, category_id, color_id);
        } else {
            ajaxList(page, area_id, keys, '', '', '');
        }
        $('.inputBtn>input').val('');
        $('.searchInput').hide();
    })

    // 下拉加载更多
    $(window).scroll(function() {
        if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
            // console.log('我到底了');
            if ($('.notMore').css('display') != "flex") {
                page++;
                let first_code = $('.first_code').attr('data-first_code');
                let category_id = $('.category_id').attr('data-category_id');
                let color_id = $('.color_id').attr('data-color_id');
                let keys = $('.keys').attr('data-keys');
                if (first_code || category_id || color_id || keys) {
                    ajaxList(page, area_id, keys, first_code, category_id, color_id)
                } else {
                    ajaxList(page, area_id, keys);
                }
            }
        }
    })

})